package com.jgc.future;

import java.util.concurrent.*;

/**
 * Created by Administrator on 2022/3/21.
 */
public class CompletableFutureTest{
    /**
     * runAsync 是没有返回值的
     * supplyAsync 是有返回值的
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,10,1L, TimeUnit.SECONDS,new LinkedBlockingQueue<>(50),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(()->{
            System.out.println(Thread.currentThread().getName() + "\t" + "-----come in");
        });
        System.out.println(future1.get());
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "------come in");
        });
        System.out.println(future2.get());
        
        CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "------come in");
            return 2;
        });
        CompletableFuture<Integer> future4 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "------come in");
            return 4;
        },threadPoolExecutor);
        System.out.println(future4.get());
        threadPoolExecutor.shutdown();
    }
}
